<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<title>实时显示日期时间</title>
</head>
<body>
<div id="time"></div>
<script type="text/javascript">
    function ShowTime(){
        var today = new Date();//创建日期对象
        var hour = today.getHours();//获取小时数
        var minu = today.getMinutes();//获取分钟数
        var seco = today.getSeconds();//获取秒数
        if(hour < 10)//如果小时数小于10
            hour ="0" + hour;//在小时数前面补0
        if(minu < 10)//如果分钟数小于10
            minu ="0" + minu;//在分钟数前面补0
        if(seco < 10)//如果秒数小于10
            seco ="0" + seco;//在秒数前面补0
        document.getElementById("time").innerHTML="-------"+today.getFullYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日"+hour+"时"+minu+"分"+seco+"秒"+"-------";
        window.setTimeout("ShowTime()",1000);//每隔1秒钟调用一次ShowTime()函数
    }
    ShowTime();//调用函数
</script>
</body>
</html>
